package com.cloud.zuul.service.impl;

import com.cloud.zuul.entity.UserEntity;
import com.cloud.zuul.mapper.UserMapper;
import com.cloud.zuul.service.UserService;
import com.github.pagehelper.PageHelper;
import com.lwj.framework.degest.DigestSignature;
import com.lwj.framework.mybatis.BaseMapper;
import com.lwj.framework.rest.PageEntity;
import com.lwj.framework.rest.RestClient;
import com.lwj.framework.security.AuthEntity;
import com.lwj.framework.util.ServletUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Transactional
@Service
public class UserServiceImpl implements UserService, UserDetailsService {
	final static Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
	@Value("${spring.application.name}")
	protected String defaultUserServerId;

	@Autowired
	protected DigestSignature digestSignature;

	@Autowired
	protected PasswordEncoder passwordEncoder;

	@Autowired
	protected RestTemplate restTemplate;

	@Autowired
	BaseMapper baseMapper;

	@Autowired
	UserMapper userMapper;

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		User user = findUserByUsername(username);
		if (user == null) {
			throw new UsernameNotFoundException(username + " is not exit.");
		}
		return user;
	}
	public User findUserByUsername(String username) throws UsernameNotFoundException {
		HttpServletRequest request = ServletUtil.getRequest();
		String password = request.getParameter("password");
		UserEntity userEntity=new UserEntity();
		userEntity.setUsername(username);
		userEntity.setPassword(password);
		List<UserEntity> query = query(userEntity);
		if(CollectionUtils.isEmpty(query)){
			throw new UsernameNotFoundException("用户不存在");
		}
        //查询用户角色，模拟数据
		List<SimpleGrantedAuthority> roleList = new ArrayList<SimpleGrantedAuthority>();
		roleList.add(new SimpleGrantedAuthority("部门经理"));
		User user = new User(username, passwordEncoder.encode(password), roleList);
		return user;
	}

	@Override
	public <T extends UserEntity> void insert(T item) {
		userMapper.insert(item);
	}

	@Override
	public <T extends UserEntity> void insert(Collection<T> list) throws Exception {
		baseMapper.batchExecute(UserMapper.class, "insert", list);
	}

	@Override
	public <T extends UserEntity> void update(T item) throws Exception {
		userMapper.updateByPrimaryKey(item);
	}

	@Override
	public <T extends UserEntity> void update(Collection<T> list) throws Exception {
		baseMapper.batchExecute(UserMapper.class, "updateByPrimaryKey", list);
	}

	@Override
	public <T extends UserEntity> void delete(T item) {
		userMapper.delete(item);
	}

	@Override
	public <T extends UserEntity> void delete(Collection<T> list) throws Exception {
		baseMapper.batchExecute(UserMapper.class, "delete", list);
	}

	@Override
	public <T extends UserEntity> List<UserEntity> query(T item) {
		return userMapper.select(item);
	}

	@Override
	public <T extends UserEntity> PageEntity<List<UserEntity>> queryPage(PageEntity<T> page) {
		PageHelper.startPage(page.getPageNum(), page.getPageSize());
		List<UserEntity> list = this.query(page.getEntity());
		PageEntity<List<UserEntity>> result = new PageEntity<>(list);
		return result;
	}

}
